package rdf

import "fmt"

type Triple struct {s, p, o *Node}

func NewTriple( s, p, o *Node ) *Triple {
  return &Triple{s, p, o}
}

func (t *Triple) S() *Node { return t.s }
func (t *Triple) P() *Node { return t.p }
func (t *Triple) O() *Node { return t.o }

func (t *Triple) String() string {
  return "{" + t.s.String() + " " + t.p.String() + " " + t.o.String() + "}"
}

func (t *Triple) Hash() int {
  return t.s.Hash() + t.p.Hash() + t.o.Hash()
}

func (t *Triple) Equals( other *Triple ) bool {
  return t.s.Equals( other.s ) && t.p.Equals( other.p ) && t.o.Equals( other.o )
}

type SetOfTriple map[int]*Triple

func NewSetOfTriples() SetOfTriple {
  return SetOfTriple(make(map[int]*Triple))
}

func (l SetOfTriple) Intersection( r SetOfTriple ) SetOfTriple {
  result := NewSetOfTriples()
  for i, t := range l { if _, present := r[i]; present { result[i] = t } }
  return result
}

func (s SetOfTriple) Add( t *Triple ) {
  h := t.Hash() 
  x, already := s[h]
  if already && !x.Equals( t ) { fmt.Printf( "oh bother!!!\n" ) } 
  s[h] = t
}
